package com.andy.beef.login.fragment

import android.content.Intent
import android.os.Bundle
import androidx.core.widget.addTextChangedListener
import androidx.navigation.fragment.findNavController
import com.andy.beef.app.util.LocalDataUtil
import com.andy.beef.databinding.FragmentPhoneLoginBinding
import com.andy.beef.login.bean.TokenBean
import com.andy.beef.login.bean.UserInfoBean
import com.andy.beef.login.viewmodel.PhoneLoginViewModel
import com.andy.beef.main.activity.MainActivity
import com.andy.module.base.BaseVbFragment
import com.andy.network.util.HttpListenerImpl
import com.andy.network.util.collectHttp
import com.blankj.utilcode.util.BarUtils
import com.andy.ui.ext.clickNoRepeat
import java.util.concurrent.TimeUnit

/**
 * @creator:LuoFan
 * @date:2022/5/26
 * @description:手机号登录
 */
class PhoneLoginFragment : BaseVbFragment<PhoneLoginViewModel, FragmentPhoneLoginBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        BarUtils.setStatusBarLightMode(requireActivity(), true)
        mViewBind.imgClose.clickNoRepeat {
            findNavController().popBackStack()
        }
        mViewBind.tvSendCode.setNormalText("获取验证码")
            .setCountDownText("重新发送", "S")
            .setIntervalUnit(TimeUnit.MILLISECONDS)
            .setOnCountDownStartListener {
                mViewBind.tvSendCode.isEnabled = false
            }
            .setOnCountDownFinishListener {
                mViewBind.tvSendCode.isEnabled = true
            }
            .clickNoRepeat {
                mViewBind.tvSendCode.startCountDown(60L)
            }
        mViewBind.tvLogin.clickNoRepeat {
            val phone = mViewBind.etPhone.text.toString()
            val code = mViewBind.etCode.text.toString()
            LocalDataUtil.saveLoginPhone(phone)
            mViewModel.loginByPhone(phone, code)
        }
        mViewBind.etPhone.setTextListener {
            mViewBind.tvSendCode.isEnabled = it.length == 11
        }
        mViewBind.etCode.addTextChangedListener {
            mViewBind.tvLogin.isEnabled = it.toString().length == 4
        }
        LocalDataUtil.getLoginPhone()?.let {
            mViewBind.etPhone.setText(it)
        }
    }

    override fun createObserver() {
        super.createObserver()
        collectHttp(mViewModel.tokenState, listener = object : HttpListenerImpl<TokenBean>() {
            override fun onSuccess(data: TokenBean) {
                LocalDataUtil.saveToken(data)
                mViewModel.getUserInfo()
            }
        })
        collectHttp(mViewModel.userInfoState, listener = object : HttpListenerImpl<UserInfoBean>() {
            override fun onSuccess(data: UserInfoBean) {
                LocalDataUtil.saveUserInfo(data)
                toMain()
            }

            override fun onError(errorCode: Int, errorMsg: String) {
                super.onError(errorCode, errorMsg)
//                ToastUtils.showShort("errorMsg=" + errorMsg)
                //TODO 目前用户信息未更新
                LocalDataUtil.saveUserInfo(UserInfoBean())
                toMain()
            }
        })
    }

    private fun toMain() {
        val intent = Intent(context, MainActivity::class.java)
        startActivity(intent)
        activity?.finish()
    }
}